package com.atguigu.springcloud.core;

import com.atguigu.springcloud.feign.AuthThreadLocal;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 请求参数设置值
 * @date 2021/10/14 11:19
 */
public class RequestCallableWrapper implements HystrixCallableWrapper {


    //获取当前线程callable请求的header参数值
    @Override
    public <T> Callable<T> wrap(Callable<T> callable) {
        return new Request<>(callable, AuthThreadLocal.getRequestMap());
    }


    static class Request<T> implements Callable<T> {

        private final Callable<T> callable;

        private final Map<String, String> requestMap;

        Request(Callable<T> callable, Map<String, String> requestMap) {
            this.callable = callable;
            this.requestMap = requestMap;
        }


        @Override
        public T call() throws Exception {
            //将获取的参数值存入map
            AuthThreadLocal.set(requestMap);
            return callable.call();
        }
    }
}
